<%@ page import="edu.cuhk.cs.bookstore.util.StringUtil" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.util.*"%>
<%@ page import="aws.*"%>
<%@ page import="aws.handler.*"%>
<%
/*
<Item>
  <ASIN>B000A2XB9U</ASIN>
  <ItemAttributes>
    <AudienceRating>NR (Not Rated)</AudienceRating>
    <Director>Lawrence Jordan (II)</Director>
    <Director>Daniel E. Catullo</Director>
    <EAN>0014381273229</EAN>
    <Format>Color</Format>
    <Format>Compilation</Format>
    <Format>NTSC</Format>
    <Languages>
      <Language>
        <Name>English</Name>
        <Type>Original Language</Type>
      </Language>
    </Languages>
    <ListPrice>
      <Amount>1999</Amount>
      <CurrencyCode>USD</CurrencyCode>
      <FormattedPrice>$19.99</FormattedPrice>
    </ListPrice>
    <NumberOfItems>1</NumberOfItems>
    <ProductGroup>DVD</ProductGroup>
    <ReleaseDate>2005-09-06</ReleaseDate>
    <RunningTime Units="minutes">131</RunningTime>
    <Studio>Image Entertainment</Studio>
    <TheatricalReleaseDate>2005-06-25</TheatricalReleaseDate>
    <Title>Rockin' the Corps</Title>
    <UPC>014381273229</UPC>
  </ItemAttributes>
</Item>
*/
%>
<% try { %>
<%

	/*
		KEYWORDS, ITEMPAGE
	 */
	String type = request.getParameter("t");
	String keywords = request.getParameter("w");
	String itemPage = request.getParameter("p");
	
	String totalPages = "0";
	String totalResults = "0";
	String printResults = "0";
	
	type = (type!=null?type:"TITLE");
	itemPage = (itemPage!=null?itemPage:"1");
	
	Map<String, String> param = new HashMap<String, String>();
	if (keywords != null & keywords.length() > 0) {
		param.put(type, keywords);
		if (itemPage != null) {
			param.put("ITEMPAGE", itemPage);
		}
	}

%><%

	List<Item> items = null;
	if (param.size() > 0) {
		ItemSearchRequestHandler service = new ItemSearchRequestHandler();
		items = service.searchItemsByKeywords(param);
		totalPages = String.valueOf(service.getTotalPages());
		totalResults = String.valueOf(service.getTotalResults());
		service = null;
	}
	
	
%>
<% if (!totalPages.equals(itemPage) && !totalPages.equals("0")) { %>
<div id="resultheader">( <%=(Integer.parseInt(itemPage) * 10) %> of <%=totalResults %> result(s) )</div>
<% } else { %>
<div id="resultheader">( Total <%=totalResults %> result(s) )</div>
<% } %>
<%
	int itemNo = (Integer.parseInt(itemPage) - 1) * 10 + 1;
	if (items != null) {
		for (Item item : items) {
			if (item != null) {
				ItemAttributes detail = item.getItemAttributes();
				String asin = item.getASIN();
				String title = detail.getTitle();
				String authors = StringUtil.join(detail.getAuthor(), ", ");
				String smallImageUrl = StringUtil.getImageURL(item.getSmallImage());
				String largeImageUrl = StringUtil.getImageURL(item.getLargeImage());
				String imageWidth = StringUtil.getImageWidth(item.getLargeImage());
				String imageHeight = StringUtil.getImageHeight(item.getLargeImage());
%>
<div class="resultItem" style="display: inline-block; height: 100px; width: 100%; border: 1px solid #EEEEEE; text-align:left">
	<table style="height: 100px">
		<tr>
			<td><%=itemNo++ %>.</td>
			<td style="width:100px;text-align:center">
				<img class="itemImage" src="<%=smallImageUrl %>" 
					asin="<%=asin %>"
					largeImage="<%=largeImageUrl %>"
					w="<%=imageWidth%>"
					h="<%=imageHeight %>"  />
			</td>
			<td> 
				<b>Title:</b> 
				<span class="itemTitle"><%=title %></span> 
				<% if (authors != null && authors.length() > 0) { %>
					by <span class="itemAuthor"><%=authors.replace(", ","</span>, <span class=\"itemAuthor\">") %></span> 
				<% } %>
			</td>
		</tr>
	</table>
</div>
<% 
			}
			else {
				//System.err.println("Item is null");
			}
		}
	}
	else {
		//System.err.println("Items is null");
	}
%>

<%  if (!totalPages.equals(itemPage) && !totalPages.equals("0")) { %>
<div id="resultfooter">
<a href="javascript:void(0)" class="moreItems" style="display: block; height: 10px; width: 100%; border: 1px solid #EEEEEE">More ...</a>
</div>
<%  } %>

<%	} catch (Exception e) { e.printStackTrace(); } %>